--[[
TD副本相关逻辑
接口列表：
sendTDInfo  刷新TD信息
up			升科技树
call		召唤怪物

onTDMonsterDie		怪物死亡回调
onTDRoleDieCB		角色死亡回调
onTDTimer			定时器回调（10秒1次）
onTDHumanBeginCopy  玩家开始副本
onTDRoomBegin			副本开始
onTDRoomClear			副本清除
--]]

local Lang = require("common.Lang")
local Msg = require("core.Msg")
local Obj = require("core.Obj")
local ObjMonster = require("core.ObjMonster")
local Copy = require("copy.CopyL")
local Aim = require("copy.Aim")
local CopyDefine = require("copy.Define")
local CopyHandlerL = require("copy.HandlerL")
local Grid = require("bag.Grid")


local TD_BOSS_X = 1746			-- 守护的怪物出生X
local TD_BOSS_Y = 1514			-- 守护的怪物出生Y
TD_BOSS = {}
TD_BOSS[CopyDefine.COPY_TD_ID1] = 1014000
TD_BOSS[CopyDefine.COPY_TD_ID2] = 1014100
TD_BOSS[CopyDefine.COPY_TD_ID3] = 1014200

local MAX_WAVE  = 30    --最大波数


bossInfo = bossInfo or {} -- boss信息 techInfo[roomID] = {id, uid} 

local function getBossHP(copyID, roomID)
	local monster = Obj.getObj(bossInfo[roomID].id, bossInfo[roomID].uid)
	if monster then	
		return monster.hp, ObjMonster.getHpMax(monster)
	end
	return 0, 1
end

local function getCurWave(copyID, roomID)
	local aimID = Aim.getDoingMainAim(copyID, roomID)
	if aimID == nil then return 0 end
		
	return aimID
end

function makeTDInfo(nowCopyID, roomID,bossHP, bossMaxHP, curWave)
	local msgRet = Msg.gc.GC_COPY_TD_INFO
	if bossHP then
		msgRet.bossHP, msgRet.bossMaxHP = bossHP, bossMaxHP
	else
		msgRet.bossHP, msgRet.bossMaxHP = getBossHP(nowCopyID, roomID)
	end
	if curWave then
		msgRet.curWave = curWave
	else
		msgRet.curWave = getCurWave(nowCopyID, roomID)
	end 
	msgRet.maxWave = MAX_WAVE
	msgRet.rewards[0] = 0
  local itemCount, itemGrids = CopyHandlerL.getRewardItem(nowCopyID)
  for i = 1, itemCount do
      msgRet.rewards[0] = msgRet.rewards[0] + 1
      Grid.makeItem(itemGrids[i], msgRet.rewards[i], 0)
      if i == 6 then
         break
      end
  end 
  return msgRet
end
-- 刷新TD信息
function sendTDInfo(human)
  local nowCopyID = Copy.isCopyBySceneID(human.scene_id)
  if nowCopyID == nil then return end
  if nowCopyID ~= CopyDefine.COPY_TD_ID1 and nowCopyID~=CopyDefine.COPY_TD_ID2 and nowCopyID~=CopyDefine.COPY_TD_ID3 then
   return
  end  
  local roomID = Copy.copyHuman2Room[human._id]
  if roomID == nil then return end
  local info = makeTDInfo(nowCopyID, roomID)
  Msg.send(info, human.fd)
end


function onTDMonsterDie(killer, monster, monsterID, copyID, roomID)
	if monsterID == TD_BOSS[copyID] then
	  local msg = makeTDInfo(copyID, roomID) 
	  Copy.sendCopy(msg, copyID, roomID)
		-- 守护失败
		Copy.roomFinish(copyID, roomID, CopyDefine.COPY_FINISH_TD_FAIL, Lang.COPY_FINISH_TD_FAIL)
		return
	end
	
end

function onTDRoleDieCB()--atker, human, copyID, roomID)
  
end

function onTDTimer(copyID, roomID)
	-- 定时刷新info	
    local msg = makeTDInfo(copyID, roomID) 
    Copy.sendCopy(msg, copyID, roomID)
end

-- 玩家进入副本的时候要初始化info
function onTDHumanBeginCopy(human)--, copyID, roomID)	
	sendTDInfo(human)
end

function onTDHumanEnterCopyScene(human)--, copyID, roomID, sceneID)
	sendTDInfo(human)
end

function onTDRoomBegin(copyID, roomID)
	local monster = ObjMonster.create(TD_BOSS[copyID])
	local mapList = Copy.copyList[copyID][roomID]
	for mapID, sceneID in pairs(mapList) do
		ObjMonster.enterScene(monster, sceneID, TD_BOSS_X, TD_BOSS_Y)
		break	
	end

	bossInfo[roomID] = {id = monster.id, uid = monster.uid}
end

function onTDRoomClear(copyID, roomID)
	bossInfo[roomID] = nil
end